home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Dev / misc / LOCCounter.lha / LOCCounter / src / File.cpp < prev    next >
C/C++ Source or Header  |  2004-08-18  |  2KB  |  100 lines

  1. /****************************************************************************
  2. *
  3. * $RCSfile: File.cpp $
  4. * $Revision: 2.7 $
  5. * $Date: 2004/08/18 21:51:05 $
  6. * $Author: ssolie $
  7. *
  8. *****************************************************************************
  9. *
  10. * Copyright (c) 2004 Steven Solie.  All Rights Reserved.
  11. *
  12. *****************************************************************************
  13. *
  14. * File component
  15. */
  16. #include "File.h"
  17.  
  18. #include <dos/dos.h>
  19. #include <utility/tagitem.h>
  20.  
  21. #include <proto/dos.h>
  22.  
  23. #include <cstring>
  24.  
  25.  
  26. File::File() :
  27.     m_handle(0),
  28.     m_info(0)
  29. {
  30. }
  31.  
  32.  
  33. File::~File()
  34. {
  35.     if ( m_info != 0 )  {
  36.         IDOS->FreeDosObject(DOS_FIB, m_info);
  37.     }
  38.  
  39.     if ( m_handle != 0 )  {
  40.         IDOS->Close(m_handle);
  41.     }
  42. }
  43.  
  44.  
  45. void File::open(const char* name, int32 mode)
  46. {
  47.     m_handle = IDOS->Open(const_cast<STRPTR>(name), mode);
  48.     if ( m_handle == 0 )  {
  49.         throw dos_error();
  50.     }
  51.  
  52.     if ( mode == MODE_OLDFILE || mode == MODE_READWRITE )  {
  53.         m_info = reinterpret_cast<FileInfoBlock*>(
  54.             IDOS->AllocDosObjectTags(DOS_FIB, TAG_END));
  55.         if ( m_info == 0 )  {
  56.             IDOS->Close(m_handle);
  57.             m_handle = 0;
  58.             IDOS->SetIoErr(ERROR_NO_FREE_STORE);
  59.             throw dos_error();
  60.         }
  61.  
  62.         if ( IDOS->ExamineFH(m_handle, m_info) == DOSFALSE )  {
  63.             IDOS->FreeDosObject(DOS_FIB, m_info);
  64.             IDOS->Close(m_handle);
  65.             m_info = 0;
  66.             m_handle = 0;
  67.             IDOS->SetIoErr(ERROR_NO_FREE_STORE);
  68.             throw dos_error();
  69.         }
  70.     }
  71. }
  72.  
  73.  
  74. int32 File::readString(char* buffer, uint32 size)
  75. {
  76.     if ( IDOS->FGets(m_handle, buffer, size) == 0 )  {
  77.         if ( IDOS->IoErr() == 0 )  {
  78.             return 0;
  79.         }
  80.         else  {
  81.             throw dos_error();
  82.         }
  83.     }
  84.  
  85.     return std::strlen(buffer);
  86. }
  87.  
  88.  
  89. uint32 File::getLength() const
  90. {
  91.     return m_info->fib_Size;
  92. }
  93.  
  94.  
  95. uint32 File::getProtection() const
  96. {
  97.     return m_info->fib_Protection;
  98. }
  99.  
  100.